﻿namespace TradePublish.SysThread.Grasp
{
    using log4net;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;
    using System.Text;
    using System.Threading;
    using SysThread.Grasp;
    using TradePublish.Entity;
    using TradePublish.Service;

    public class GraspTMImageThread
    {
        private Thread doThread;
        private ILog log = null;
        private List<GraspTMImageSubThread> subThreads = new List<GraspTMImageSubThread>();
        private TradePublishManager tradePublishManager = new TradePublishManager();

        public GraspTMImageThread()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public void start()
        {
            for (int i = 0; i < 10; i++)
            {
                GraspTMImageSubThread item = new GraspTMImageSubThread {
                    id = i,
                    isRunning = false
                };
                item.start();
                this.subThreads.Add(item);
            }
            this.log.Info("已开启10个采集商标图片的子线程.");
            this.doThread = new Thread(new ThreadStart(this.threadWork));
            this.doThread.IsBackground = true;
            this.doThread.Start();
        }

        private void threadWork()
        {
            int readySubThreadCount = 10;//当前有几个子线程等待任务？
            while (true)
            {
                try
                {
                    #region 看看有多少个子线程等待分配任务
                    while (true)
                    {
                        readySubThreadCount = 0;
                        for (int x = 0; x < subThreads.Count; x++)
                        {
                            if (!subThreads[x].isRunning)
                            {
                                //该进程已停止了。
                                readySubThreadCount++;
                            } 
                        }
                        if (readySubThreadCount == 0)
                        {
                            log.Info("无子线程空闲");
                            Thread.Sleep(15000);//休息15秒
                        }
                        else
                        {
                            log.Info("有" + readySubThreadCount + "个子线程空闲,准备分配任务.");
                            break;
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    log.Error("CCCCCCCCCC111终于抓住莫名异常：", ex);
                    continue;
                }
                try
                {
                    DataSet ds = tradePublishManager.findTopUnGraspTMImage(readySubThreadCount * 10 );//内部异常自己处理
                    if (ds == null || ds.Tables == null || ds.Tables.Count == 0 || ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count == 0)
                    {
                        //没有需要处理的
                        log.Info("有需要采集图片的商标0个.");
                        Thread.Sleep(120000);//休息2分钟
                        continue;
                    }
                    else
                    {
                        //下发任务到子线程中。
                        log.Info("有需要采集商标图片的商标共" + ds.Tables[0].Rows.Count + "个.");
                        List<TradePublishVo> l = new List<TradePublishVo>();
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            TradePublishVo vo = new TradePublishVo();
                            #region copy属性
                            vo.id = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"]);
                            vo.issue = Convert.ToInt32(ds.Tables[0].Rows[i]["ISSUE"]);
                            vo.regNo = ds.Tables[0].Rows[i]["REG_NO"].ToString();
                            vo.classNo = Convert.ToInt32(ds.Tables[0].Rows[i]["CLASS_NO"].ToString());
                            vo.category = Convert.ToInt32(ds.Tables[0].Rows[i]["CATEGORY"]);
                            #endregion
                            l.Add(vo);
                            if (l.Count >= 10 || i == ds.Tables[0].Rows.Count - 1)
                            {
                                //给子线程任务
                                for (int x = 0; x < subThreads.Count; x++)
                                {
                                    if (!subThreads[x].isRunning)
                                    {
                                        subThreads[x].tradePublishs = l;
                                        subThreads[x].isRunning = true;
                                        log.Info("需要采集商标图片的任务分配给GraspTMImageSubThread-" + subThreads[x].id + ".");
                                        l = new List<TradePublishVo>();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error("CCCCCCCCCC222终于抓住莫名异常：", ex);
                    continue;
                }
            }
        }
    }
}

